package com.example.demo.service.serviceImpl;

import com.example.demo.Exception.BackErrorException;
import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Login;
import com.example.demo.entity.User;
import com.example.demo.entity.requestJson.UregisterReq;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.mapper.RegistMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.URegistService;
import com.example.demo.tools.Md5utils;
import com.example.demo.tools.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class URegistServiceImpl implements URegistService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RegistMapper registMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 读者端注册，默认role为1
     * */
    @Override
    public BackResponseEntity<Object> userRegist (UregisterReq uregisterReq) throws BackErrorException{
        String phone = null;
        String code = null;
        String pwd = null;
        String confirmpwd = null;


        if (!ObjectUtils.isEmpty(uregisterReq.getRegisterPhone())) {
        phone =uregisterReq.getRegisterPhone()
                .trim();
        }
        if (!ObjectUtils.isEmpty(uregisterReq.getVerifyCode())) {
        code =uregisterReq.getVerifyCode()
                .trim();
        }
        if (!ObjectUtils.isEmpty(uregisterReq.getLoginPwd())) {
        pwd =uregisterReq.getLoginPwd()
                .trim();
        }
        if (!ObjectUtils.isEmpty(uregisterReq.getConfirmLoginPwd())) {
        confirmpwd =uregisterReq.getConfirmLoginPwd()
                .trim();
        }


        if(ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(code) || ObjectUtils.isEmpty(pwd) || ObjectUtils.isEmpty(confirmpwd)
          ){

            return BackResponseEntity.success(ErrCodeEnum.unknown_4006,"参数缺失");
        }

        Object verifyDataObject = redisUtil.get(phone + "SMS:users");
        if (!ObjectUtils.isEmpty(verifyDataObject)) {
            String verifyData = (String) verifyDataObject;
            if (!code.equals(verifyData)) {
                return BackResponseEntity.success(ErrCodeEnum.unknown_1001,"验证码错误");
            }
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1000,"验证码过期");
        }
        if(!confirmpwd.equals(pwd)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1002,"两次输入密码不一致");
        }

        String loginPwd = Md5utils.getmd5Lowercase("qingyu" + pwd);
        String nickname = "小猫咪"+ UUID.randomUUID().toString().replace("-","").substring(0,5);
        Login login = new Login();
        Login loginres = loginMapper.queryLoginLimit1(login);

        if(!ObjectUtils.isEmpty(loginres) && phone.equals(loginres.getLoginPhone())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1003,"该手机号已注册，请直接登录");
        }

        LocalDateTime time = LocalDateTime.now();
        String pic = "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=128476812,2256985719&fm=26&gp=0.jpg";
        login.setLoginPwd(loginPwd);
        login.setLoginNickname(nickname);
        login.setLoginPhone(phone);
        login.setRegistTime(time);
        login.setLoginRole(1);
        login.setLoginPic(pic);

        int i = loginMapper.insert(login);

        Login registedRes = registMapper.queryRegisted(phone);

        User user = new User();
        user.setUserId(registedRes.getLoginId());
        user.setUserNickname(nickname);
        user.setUserPhone(phone);
        user.setUregistTime(time);
        user.setUserImg(pic);

        userMapper.insert(user);


        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0,"恭喜您注册成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1004,"注册失败");
        }

    }
}
